#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'
import douban.service
import atom
import douban
from douban.client import OAuthClient

class Douban(object):
    def __init__(self, key, secret):
        self.consumer_key = key
        self.consumer_secret = secret

    def auth(self):
        self.client = OAuthClient(key=self.consumer_key, secret=self.consumer_secret)
        return self.client.login()

    def setToken(self, token, tokenSecret):
        server = 'api.douban.com'
        self.client = douban.service.DoubanService(server = server, api_key = self.consumer_key, secret = self.consumer_secret)
        self.client.ProgrammaticLogin(token_key = token, token_secret = tokenSecret)

    def write(self, words):
        entry = douban.BroadcastingEntry()
        entry.content = atom.Content(text = words)
        return self.client.AddBroadcasting("/miniblog/saying", entry)

if __name__ == "__main__":
    import config
    d = Douban(config.douban_consumer_key, config.douban_consumer_secret)
    if not config.douban_token_key:
        d.auth()
        print 'write token_key and token_secret'
    else:
        d.setToken(config.douban_token_key, config.douban_token_secret)
        r = d.write('lala')
        if r:
            print 'douban ok'

